package com.starbaba.stat.view;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.starbaba.core.utils.PropertyParamMapBuilder;
import com.starbaba.core.utils.Struts2Utils;
import com.starbaba.core.utils.json.JsonUtils;
import com.starbaba.core.web.page.PageResponse;
import com.starbaba.core.web.struts2.BaseAction;
import com.starbaba.stat.model.StatFlowConverModel;
import com.starbaba.stat.service.StatFlowConverService;

@SuppressWarnings({"rawtypes", "unchecked", "serial"})
public class StatFlowConverServiceAction extends BaseAction implements ModelDriven<StatFlowConverModel>{

	private StatFlowConverModel model;
	private StatFlowConverService service;
	
	private InputStream input;

	private String fileName;
	
	public InputStream getInput() {
		return input;
	}

	public void setInput(InputStream input) {
		this.input = input;
	}

	public String getFileName() {
		return fileName;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}
	
	@Autowired
	@Qualifier("statFlowConverServiceImpl")
	public void setService(StatFlowConverService service) {
		this.service = service;
	}

	public String showPage() {
		paramsMap = PropertyParamMapBuilder.buildFromHttpRequest(Struts2Utils.getRequest(), paramsMap);
		try {
			service.showPage(paramsMap);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ActionSupport.SUCCESS;
	}
	
	public void queryList(){
		paramsMap = PropertyParamMapBuilder.buildFromHttpRequest(Struts2Utils.getRequest(), paramsMap);
		parsePageRequest();
		PageResponse pageResponse = new PageResponse();
		int start = pageRequest.getIDisplayStart();
		int length = pageRequest.getIDisplayLength();
		
		try {
			List list = service.getList(start, length, pageResponse, paramsMap);
			pageResponse.setAaData(list);
			pageResponse.setSEcho(pageRequest.getSEcho());
			
			sendJsonMessage(JsonUtils.toJson(pageResponse, null, true, null, false));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public String exportCVS() throws Exception {
		paramsMap = PropertyParamMapBuilder.buildFromHttpRequest(Struts2Utils.getRequest(), paramsMap);
		parsePageRequest();
		PageResponse pageResponse = new PageResponse();
		String value = exportCVS(service.getList(null, null, pageResponse, paramsMap));
		input = new ByteArrayInputStream(value.getBytes("GBK"));
		fileName = System.currentTimeMillis() + ".csv";
		return ActionSupport.SUCCESS;
	}
	
	public String exportCVS(List<StatFlowConverModel> list) {
		StringBuilder builder = new StringBuilder();
		builder.append("日期,平台,产品,渠道,用户类型,所属页面,所属模块,位置值,内容id,内容名,点击UV,点击PV,参与UV,参与PV,参与转化率\r\n");
		for (StatFlowConverModel vo : list) {
			builder.append(vo.getTime()).append(","); 
			builder.append(vo.getPlatform()).append(",");
			builder.append(vo.getPrd_name()).append(",");
			builder.append(vo.getChannel_name()).append(",");
			builder.append(vo.getUser_type()).append(",");
			builder.append(vo.getPage_name()).append(",");
			builder.append(vo.getCk_module_name()).append(",");
			builder.append(vo.getPosition_name()).append(",");
			builder.append(vo.getContentid()).append(",");
			builder.append(vo.getFunctionid_name()).append(",");
			builder.append(vo.getClick_uv()).append(",");
			builder.append(vo.getClick_pv()).append(",");
			builder.append(vo.getJoin_uv()).append(",");
			builder.append(vo.getJoin_pv()).append(",");
			builder.append(vo.getConver_rate()).append(",");
			builder.append("\r\n");
		}
		return builder.toString();
	}
	
	
	@Override
	public StatFlowConverModel getModel() {
		if (model == null) {
			this.model = new StatFlowConverModel();
		}
		return model;
	}
}
